//
//  MainViewController.m
//  Wallets Manager
//
//  Created by Nguyen Chi Cong on 9/13/12.
//  Copyright (c) 2012 BKHN. All rights reserved.
//

#import "MainViewController.h"
#import "WalletsViewController.h"
#import "GeneralManagerViewController.h"
#import "ReportViewController.h"
#import "PlanViewController.h"

@implementation MainViewController

//@synthesize walletsViewController;
@synthesize tabBarController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    gTabBarController = [[UITabBarController alloc]init];
    NSMutableArray *controllers = [[NSMutableArray alloc]init];
    
    GeneralManagerViewController *gmvc = [[GeneralManagerViewController alloc] initWithNibName:@"GeneralManagerViewController" bundle:nil];
    gmvc.tabBarItem.image = [UIImage imageNamed:@"money-01.png"];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:gmvc];
    [nav1 setNavigationBarHidden:YES];
    [controllers addObject:nav1];
    
    ReportViewController *rvc = [[ReportViewController alloc] initWithNibName:@"ReportViewController" bundle:nil];
    rvc.tabBarItem.image = [UIImage imageNamed:@"chart-01.png"];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:rvc];
    [nav2 setNavigationBarHidden:YES];
    [controllers addObject:nav2];
    
    PlanViewController *pvc = [[PlanViewController alloc] initWithNibName:@"PlanViewController" bundle:nil];
    pvc.tabBarItem.image = [UIImage imageNamed:@"save-01.png"];
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:pvc];
    [nav3 setNavigationBarHidden:YES];
    [controllers addObject:nav3];
    
    ToolsViewController *tvc = [[ToolsViewController alloc] initWithNibName:@"ToolsViewController" bundle:nil];
    tvc.tabBarItem.image = [UIImage imageNamed:@"tool-01.png"];
    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:tvc];
    [nav4 setNavigationBarHidden:YES];
    [controllers addObject:nav4];
    
    SettingsViewController *svc = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
    svc.tabBarItem.image = [UIImage imageNamed:@"settings-01.png"];
    UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:svc];
    [nav5 setNavigationBarHidden:YES];
    [controllers addObject:nav5];
    
    gTabBarController.viewControllers = controllers;
    gTabBarController.customizableViewControllers = controllers;
    [gTabBarController setSelectedViewController:nav1];
    gTabBarController.delegate = self;
    
//    if(walletsViewController == nil)
//    {
//        walletsViewController = [[WalletsViewController alloc] initWithNibName:@"WalletsViewController" bundle:nil];
//         walletsViewController.delegate = self;
//    }
//    [self performSelector:@selector(loadWallets:) withObject:walletsViewController afterDelay:1];
    [self performSelector:@selector(pushToWalletController) withObject:nil afterDelay:1];
}

-(void)loadWallets: (UIViewController *)controller
{
    [self.view addSubview:controller.view];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    //walletsViewController   = nil;
    tabBarController        = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) pushToWalletController {
    
    WalletsViewController *walletsViewController = [[WalletsViewController alloc]init];
    [UIView beginAnimations:@"animation" context:nil];
    [self.navigationController pushViewController:walletsViewController animated:YES];
    [UIView setAnimationDuration:0.8];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];
}

#pragma mark - WalletDelegate

-(void)walletDidSelected:(id)wallet
{
    NSLog(@"%@", wallet);
    if(tabBarController == nil)
    {
        tabBarController = [[WalletManagerController alloc] init];
        [tabBarController initWithComponents];
    }
    //[walletsViewController.view removeFromSuperview];
    
    [self.view addSubview:tabBarController.view];
}

@end
